package com.neusoft.restaurant.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.restaurant.config.utils.PageUtils;
import com.neusoft.restaurant.config.utils.RedisUtils;
import com.neusoft.restaurant.entity.PageResult;
import com.neusoft.restaurant.entity.ResShop;
import com.neusoft.restaurant.mapper.ResShopMapper;
import com.neusoft.restaurant.service.IResShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 店铺Service业务层处理
 * 
 * @author zhongda
 * @date 2023-10-13
 */
@Service
public class ResShopServiceImpl implements IResShopService
{


    @Autowired
    private ResShopMapper resShopMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询店铺
     * 
     * @param id 店铺主键
     * @return 店铺
     */
    @Override
    public ResShop selectResShopById(Long id)
    {
        if (redisUtils.exists("shop"+id)){
            return (ResShop) redisUtils.get("shop"+id);
        }else {
            ResShop resShop= resShopMapper.selectResShopById(id);
            redisUtils.set("shop"+id,resShop);
            return resShop;
        }
    }

    /**
     * 查询店铺列表
     * 
     * @param resShop 店铺
     * @return 店铺
     */
    @Override
    public List<ResShop> selectResShopList(ResShop resShop)
    {
        return resShopMapper.selectResShopList(resShop);
    }

    @Override
    public PageResult selectResShopList2(ResShop resShop, Integer pageNum, Integer pageSize) {
        //1、PageHelper开始分页，传入第几页和每页多少条记录两个参数
        PageHelper.startPage(pageNum, pageSize);
        //2、数据库进行查询
        List<ResShop> resShopes = resShopMapper.selectResShopList(resShop);
        //3、对返回值进行判断
        if (resShopes.size()==0){
            return null;
        }
        //4、对查询的结果进行封装
        PageInfo<ResShop> info = new PageInfo<>(resShopes);
        PageResult pageResult = PageUtils.getPageResult(info);

        return pageResult;
    }

    /**
     * 新增店铺
     * 
     * @param resShop 店铺
     * @return 结果
     */
    @Override
    public int insertResShop(ResShop resShop)
    {
        resShop.setCreateTime(new Date());
        return resShopMapper.insertResShop(resShop);
    }

    /**
     * 修改店铺
     * 
     * @param resShop 店铺
     * @return 结果
     */
    @Override
    public int updateResShop(ResShop resShop)
    {
        resShop.setUpdateTime(new Date());
        return resShopMapper.updateResShop(resShop);
    }

    /**
     * 批量删除店铺
     * 
     * @param ids 需要删除的店铺主键
     * @return 结果
     */
    @Override
    public int deleteResShopByIds(Long[] ids)
    {
        return resShopMapper.deleteResShopByIds(ids);
    }

    /**
     * 删除店铺信息
     * 
     * @param id 店铺主键
     * @return 结果
     */
    @Override
    public int deleteResShopById(Long id)
    {
//        long[] ids={id};

        return resShopMapper.deleteResShopById(id);
    }
}
